package com.cms.facade;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import com.cms.exception.ExceptionInfo;

public class RequestFacade {

	public static List getResultList(HttpServletRequest req) {
		Object obj = req.getAttribute(HttpAttribute.RESULT_LIST);
		if (obj == null) {
			return null;
		} else {
			return (List) obj;
		}
	}

	public static void setResultList(HttpServletRequest req, List list) {
		req.setAttribute(HttpAttribute.RESULT_LIST, list);
	}
	
	public static int getResultTotal(HttpServletRequest req) {
		Object obj = req.getAttribute(HttpAttribute.RESULT_TOTAL);
		if (obj == null) {
			return 0;
		} else {
			return Integer.parseInt((String) obj);
		}
	}

	public static void setResultTotal(HttpServletRequest req, int count) {
		req.setAttribute(HttpAttribute.RESULT_TOTAL, String.valueOf(count));
	}

	public static Boolean getSuccessFlag(HttpServletRequest req) {
		Object obj = req.getAttribute(HttpAttribute.SUCCESS_FLAG);
		if (obj == null) {
			return Boolean.FALSE;
		} else {
			return (Boolean) obj;
		}
	}

	public static void setSuccessFlag(HttpServletRequest req, Boolean flag) {
		req.setAttribute(HttpAttribute.SUCCESS_FLAG, flag);
	}

	public static String getErrorMsg(HttpServletRequest req) {
		Object obj = req.getAttribute(HttpAttribute.ERROR_MSG);
		if (obj == null) {
			return "";
		} else {
			return (String) obj;
		}
	}

	public static void setErrorMsg(HttpServletRequest req, String msg) {
		req.setAttribute(HttpAttribute.ERROR_MSG, msg);
	}

	public static void setExceptionInfo(HttpServletRequest req,
			ExceptionInfo exceptionInfo) {
		req.setAttribute(HttpAttribute.Eccm_ExceptionInfo, exceptionInfo);
	}

	public static ExceptionInfo getExceptionInfo(HttpServletRequest req) {
		Object obj = req.getAttribute(HttpAttribute.Eccm_ExceptionInfo);
		if (obj == null)
			return null;
		else
			return (ExceptionInfo) obj;
	}

	public static void setErrorCode(HttpServletRequest req, int return_code) {
		req.setAttribute(HttpAttribute.ERROR_CODE, return_code);
	}
	
	public static int getErrorCode(HttpServletRequest req) {
		Object obj = req.getAttribute(HttpAttribute.ERROR_CODE);
		if (obj == null) {
			return 0;
		} else {
			return (Integer) obj;
		}
	}
	
    public static void setInvalidTokenMsg(HttpServletRequest req) {
        req.setAttribute(HttpAttribute.ERROR_MSG, "Invalid token possibly caused by page reload");
        req.setAttribute(HttpAttribute.SUCCESS_FLAG, Boolean.FALSE);
    }
}
